var nodeid="";
var currentNodeName="";
var parindex="";
var administratorid="";
var administratorname="";

/**
 * 加载类别维护，左边树结构
 */
var categoryMaintance = {
	/**
	 * 初始化左边树结构
	 */
	initLeftTree:function(){
		//调用后台接口，取得树形结构的列表
		//转圈
		common.showLoading();
		var url = "/PCKnowledgeCategoryEntity/getAllKnowledgeCategory";
		common.interfaceAjaxFunction(url,"",this.showLeftPartTreeSuccess);
		
	},
	/**
	 * end初始化左边树结构
	 */
	
	
	/**
	 * 调用取得树的结构,然后进行数据的渲染
	 */
	showLeftPartTreeSuccess:function(json){
		//成功时候
    	if(json.status == "0"){
    		if(json.data.length > 0){
	    		//加载设置后的主页面
    			categoryMaintance.showLeftPartTree(json.data);
    		}else{
    			//加载设置页面
    			common.show_error("当前无行业树结构数据，请联系管理员维护");
    		}
    	}else{
    		//给出信息提示
    		common.show_error(json.message);
    	}
    	//转圈提示去除
    	common.hideLoading();
	},
	/**
	 * end调用取得树的结构,然后进行数据的渲染
	 */
	
	/**
	 * 显示showLeftPartTree函数，渲染显示数据
	 */
	showLeftPartTree:function(jsonData){
		var setting = {
				data: {
					key: {
						title:"title"
					},
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: onClick
				}
			};

			var zNodes =[];
			for(var i=0;i<jsonData.length;i++){
				var jsonDataRow = jsonData[i];
				var jsonStruct = {
					id:	jsonDataRow.pk_knowledge_category,
					pId:jsonDataRow.fw_parent,
					name:jsonDataRow.knowledge_category_name,
					title:jsonDataRow.knowledge_category_name,
					level:jsonDataRow.fw_level,
					user:jsonDataRow.pk_user,
					username:jsonDataRow.user_name,
					sealmark:jsonDataRow.sealmark
				};
				//压入到树结构中去
				zNodes.push(jsonStruct);
			}
		
			function onClick(event, treeId, treeNode, clickFlag) {
				  
				if(treeNode.level==0){
					$("#currentNodeName1").val(treeNode.name);
					$("#currentNodeName").val("无");
					$("#thisbefornodeid").val(treeNode.id);
					$("#liststatus").val(treeNode.sealmark);
					var data={
							"pk_knowledge_category":treeNode.id,
							"pk_user":window.localStorage.userid
					}
					var url="/PCKnowledgeCategoryEntity/getAllKnowledgeCategoryRelation";
					common.showLoading();
					common.interfaceAjaxFunction(url,data,categoryMaintance.showpkuser);
					$("#add").show();
				}else{
					//上级类别
					$("#currentNodeName").val(treeNode.getParentNode().name);
					$("#thisbefornodeid").val(treeNode.id);
					$("#liststatus").val(treeNode.sealmark);
					var data={
							"pk_knowledge_category":treeNode.id,
							"pk_user":window.localStorage.userid
					}
					var url="/PCKnowledgeCategoryEntity/getAllKnowledgeCategoryRelation";
					common.showLoading();
					common.interfaceAjaxFunction(url,data,categoryMaintance.showpkuser);
					$("#add").hide();
				}
				
				//当前类别名称
				$("#thisbefornodename2").val(treeNode.name);
				
			}		
			//删除  
			
//			$.fn.zTree.init($("#leftTreeCategoryMaintance"), setting, zNodes);
	},
	showpkuser:function(json){
		$("#administratorname").val("");
		$("#administratorid").val("");
		if(json.status=="0"){
			$("#administratorname").val(json.data.user_name);
			$("#administratorid").val(json.data.pk_user);
		}
		common.hideLoading();
	},
	//删除的回调函数
	deleteTreeSuccess:function(json){
		if(json.status=="0"){
			layer.msg(json.message);
			categoryMaintance.initLeftTree();
			$("#currentNodeName").val("");
			$("#thisbefornodename2").val("");
			$("#liststatus").val("");
			$("#administratorname").val("");
			$("#administratorid").val("");
		}else if(json.status=="1"){
			layer.msg(json.message);
		}else if(json.status=="2"){
			layer.msg(json.message);
		}else if(json.status=="3"){
			layer.msg(json.message);
		}
	},
	//新增成功
	saveSuccess:function(json){
		if(json.status=="0"){
			layer.msg("新增成功！");
			categoryMaintance.initLeftTree();
			$("#currentNodeName").val("");
			$("#thisbefornodename2").val("");
			$("#administratorname").val("");
			$("#liststatus").val("Y");
			$("#add").show();
			//$("#liststatus").val("");
		}else{
			layer.msg("新增失败！");
		}
		
	},
	updateSuccess:function(json){
		if(json.status=="0"){
			layer.msg("修改成功！");
			categoryMaintance.initLeftTree();
			$("#currentNodeName").val("");
			$("#thisbefornodename2").val("");
			$("#administratorname").val("");
			$("#liststatus").val("Y");
			$("#add").show();
			//$("#liststatus").val("");
		}else{
			layer.msg("您没有权限，无法修改");
		}
		
	},
	/**
	 * 事件的绑定
	 */
	initEvenBind : function(){
	 	$("#add").unbind().on("click",function(){
			nodeid=$("#thisbefornodeid").val();
			/*if(id==""){
				layer.msg("请选择要添加的知识类别！");
			}else{
				$("#thisbefornodename2").val("");
				$("#thisbefornodename2").val("");
			}*/
			currentNodeName=$("#currentNodeName1").val();
			//window.sessionStorage.nodeid =id;
			//window.sessionStorage.currentNodeName =currentNodeName;
			layer.open({
			    type: 2,
			    title: '新增商品',
			    fix: false,
			    shadeClose: true,
			    maxmin: false,
			    area: ['1000px', '500px'],
			    end : function(){
			    	categoryMaintance.initLeftTree();
			    	$("#administratorname").val("");
			    	$("#thisbefornodename2").val("");
			    	$("#currentNodeName").val("");
			    	
				  },
			    content:'html/categoryMaintanceAdd.html',
			 });
		});
		//删除
		$("#delete").unbind().on("click",function(){
			var id=$("#thisbefornodeid").val();
			var thisbefornodename2=$("#thisbefornodename2").val();
			if(id==""){
				layer.msg("请选择要删除的商品！");
			}else{
				layer.confirm("您确认要删除"+thisbefornodename2+"吗？", {area: ['400px', '200px'],title:'删除'}, function(){
					var pk_knowledge_category=$("#thisbefornodeid").val();
					var url = "/PCKnowledgeCategoryEntity/deleteByPrimaryKey";
					var data = {"knowledgeCategory": pk_knowledge_category};
					common.interfaceAjaxFunction(url,data,categoryMaintance.deleteTreeSuccess);
				});
			}
		});
		
		//修改节点
		$("#save").unbind().on("click",function(){
			nodeid=$("#thisbefornodeid").val();
			currentNodeName=$("#currentNodeName").val();
			var knowledge_category_name= $("#thisbefornodename2").val();
			 var len = 0;    
			    for (var i=0; i<knowledge_category_name.length; i++) {    
			        if (knowledge_category_name.charCodeAt(i)>127 || knowledge_category_name.charCodeAt(i)==94) {    
			             len += 2;    
			         } else {    
			             len ++;    
			         }    
			     } 
			 
			var liststatus= $("#liststatus").val();
			var administrator= $("#administratorid").val();
			if(knowledge_category_name==""){
				layer.msg("请填写商品名称");
			}else if(administrator==""){
				layer.msg("请选择类别管理员");
			}else if(len>60){
				layer.msg("你的输入过长，请重新输入！");
			}else if(currentNodeName!=""){
				 var url = "/PCKnowledgeCategoryEntity/updateByPrimaryKeySelective";
					var data={
							"categoryId":nodeid,
							"knowledge_category_name":knowledge_category_name,
							"pk_user":administrator,
							"sealmark":liststatus,
							"upate_user":window.localStorage.userid
					};
		        	common.interfaceAjaxFunction(url,data,categoryMaintance.updateSuccess);
			}else{
				 var url = "/PCKnowledgeCategoryEntity/insertCategory";
					var data={
							"fw_parent":nodeid,
							"knowledge_category_name":knowledge_category_name,
							"pk_user":administrator,
							"sealmark":liststatus
					};
					common.interfaceAjaxFunction(url,data,categoryMaintance.saveSuccess);
			}
			
		});
		
		//人员选择
		$("#check").unbind().on("click",function(){
		   administratorid=$("#administratorid").val();
		   administratorname=$("#administratorname").val();
		   //window.sessionStorage.administratorid=administratorid;
		   //window.sessionStorage.administratorname=administratorname;
		   layer.ready(function () {
				  //增加编辑
			   layer.open({
				    type: 2,
				    //skin: 'layui-layer-lan',
				    title: "选择人员",
				    fix: false,
				    shadeClose: true,
				    maxmin: true,
				    area: ['1000px', '600px'],
				    content: 'html/updateemployee.html',
			  });
			});
		});
	},
	/**
	 * 初始化窗口高度
	 */
	initPageHeight:function(){
		//整体高度
		var winHeight = $(window).height();
		//top高度
		var winTopHeight = $(".shell-top-box").height();
		//左边的高度为
		$(".leftHeight").height(winHeight-winTopHeight-$("#tabHdLeftTitle").height() - 30);
		//设定宽度
		var leftWidth = $("#layout-t").width();
		$(".leftHeight").width(leftWidth);
	},
	initialize : function(){
		categoryMaintance.initLeftTree();
		categoryMaintance.initEvenBind();
		categoryMaintance.initPageHeight();
	}
	
};

/**
 * 根据搜索框内容，过滤产品列表
 */
var searchKeyList = function(obj){
	//输入值
	var value = $(obj).val();
	//输入框对应id
	var thisInputId = $(obj).attr("id");
	//对应过滤id
	var $filterList = "";
	//根据id分别给出，适应过个输入框情况
	if(thisInputId == "chantext"){
		$filterList = $('#chanping').find('li');//获取产品列表
	}else if(thisInputId == "jingtext"){
		$filterList = $('#jingping').find('li');//获取产品列表
	}
    //
	$filterList.css('display', 'block');
	//过滤
	$filterList.each(function(index, elem){
		//循环过滤
		if($(this).text().indexOf(value) == -1){//含有该搜索字符串
			$(this).css('display', 'none');
		}
	})
};
